<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Banner管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <label for="keyword"></label><input id="keyword" type="text" name="keyword"
                                                                    autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="searchBtn" type="button" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn">
                                <i class="layui-icon"></i> 搜 索
                            </button>
                            <button id="delBtn" type="button" class="layui-btn layui-btn-normal"><i class="layui-icon">&#xe640;</i>回收站</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add" >添加Banner</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="start">展示</button>
                <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="stop">取消展示</button>
            </div>
        </script>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn layui-btn-radius layui-btn-xs" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger" lay-event="del">删除</a>
        </script>

        <script type="text/html" id="currentTableBar1">
            <a class="layui-btn layui-btn layui-btn-radius layui-btn-xs" lay-event="regain">恢复</a>
        </script>

        <script type="text/html" id="bannerForm">
            <form class="layui-form" action="" lay-filter="bannerForm">
                <input type="hidden" id="id" name="id">

                <div class="layui-form-item">
                    <label class="layui-form-label">Banner名称</label>
                    <div class="layui-input-inline">
                        <label><input type="text" name="name" lay-verify="required" placeholder="请输入Banner名称"
                                      autocomplete="off" class="layui-input"></label>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <label><input type="hidden" name="img" id="img"></label>
                        <button type="button" class="layui-btn layui-btn-normal" id="bImg"><i class="layui-icon"></i>上传Banner图</button>
                    </div>
                    <div class="layui-input-block">
                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            预览图：
                            <div class="layui-upload-list" id="imgList"></div>
                        </blockquote>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">排序规则</label>
                    <div class="layui-input-inline">
                        <label><input type="text" name="sort" lay-verify="required|number" placeholder="请输入排序数字[数值越大排序越靠前]"
                                      autocomplete="off" class="layui-input"></label>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button id="saveBtn" type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </script>

    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js" charset="utf-8"></script>
<script th:inline="none">
    layui.use(['form', 'table', 'layer', 'upload', 'common'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            upload = layui.upload;

        // 搜索按钮绑定事件
        $("#searchBtn").on("click", function () {
            // table表格渲染数据
            table.render({
                elem: '#currentTableId',
                url: '/banner/list', // 发送请求的地址
                toolbar: '#toolbarDemo',
                where: {
                    keyword: $("#keyword").val()
                },
                cols: [[
                    {type: 'checkbox', field: 'left'},
                    {field: 'id', title: 'ID', width:80, sort: true},
                    {field: 'name', title: 'Banner名称'},
                    {field: 'img', title: 'Banner图', templet: function (d) {
                        return "<img src=" + d.path + " width='77px' height='44px' class='layui-card'>"
                    }},
                    {field: 'show', title: '是否展示在主页', style: 'color: deeppink;'},
                    {field: 'createtime', title: '上传时间'},
                    {title: '操作', toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            }); //end table.render
        }).click(); //end $("#searchBtn")

        // 搜索按钮绑定事件
        $("#delBtn").on("click", function () {
            // table表格渲染数据
            table.render({
                elem: '#currentTableId',
                url: '/banner/del/list', // 发送请求的地址
                where: {
                    keyword: $("#keyword").val()
                },
                cols: [[
                    {type: 'checkbox', field: 'left'},
                    {field: 'id', title: 'ID', width:80, sort: true},
                    {field: 'name', title: 'Banner名称'},
                    {field: 'img', title: 'Banner图', templet: function (d) {
                            return "<img src=" + d.path + " width='58px' height='36px' class='layui-card'>"
                        }},
                    {field: 'createtime', title: '上传时间'},
                    {title: '操作', toolbar: '#currentTableBar1', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            }); //end table.render
        }); //end $("#searchBtn")


        let uploadFile = function () {
            upload.render({
                elem: '#bImg',
                url: '/banner/imgs',
                accept: 'file',  //普通文件
                before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#imgList').append('<img width="50px" height="50px" src="' + result + '" alt="' + file.name + '" class="layui-upload-img">')
                    });
                },
                done: function (res) {
                    if (res.code === 0) {
                        $("#img").val('');
                        $("#img").val(res.data.src);
                    } // end if
                    infoMsg("上传成功！");
                } //end done
            }); //end upload
        }

        // 保存按钮绑定事件
        $("body").on("click", "#saveBtn", function () {
            // 打包form表单中的所有数据
            let data = form.val("bannerForm");
            // 发送请求把数据推送后台
            $.post("/banner/save", data, function () {
                // 请求成功后  1、关窗口，2、点搜索
                layer.closeAll();
                $("#searchBtn").click();
            }); //end $.post

        }); //end $("#saveBtn")

        // 提示信息
        let infoMsg = function (info) {
            layer.msg(info, {
                icon: 1,
                time: 1200
            });
        }
        let infoMsgError = function(info) {
            layer.msg(info, {
                icon: 2,
                time: 1200
            });
        }

        // 表格操作工具条时间
        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') { // 点的是编辑
                $.post("/banner/edit/" + obj.data.id, function (b) {
                    layer.open({
                        type: 1,
                        title: "修改Banner",
                        area: ["520px", "600px"],
                        content: $("#bannerForm").html()
                    });
                    // 数据回显
                    form.val("bannerForm", b);
                    uploadFile();
                }); //end $.post
                // 渲染表单
                form.render();
            } //end if

            if (obj.event === 'del') { // 点的是删除
                layer.confirm("您确定要删除该Banner吗？", function () {
                    $.post("/banner/del/" + obj.data.id, function () {
                        // 关窗口
                        layer.closeAll();
                        infoMsg("删除成功！");
                        // 重现渲染一遍table表格数据
                        $("#searchBtn").click();
                    }); //end $.post
                }); //end layer.confirm
            } // end if

            if (obj.event === 'regain') {
                $.post("/banner/regain/" + obj.data.id, function () {
                    infoMsg("恢复成功！");
                })
            }

        }); //end tool(currentTableFilter)


        //监听表格复选框选择
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'start') { // 点的是上架
                layer.confirm("您确定要展示这些Banner吗？", function () {
                    let checkStactus = table.checkStatus('currentTableId');
                    let data = checkStactus.data;
                    let ids = [];
                    $.each(data, function (i, v) {
                        ids[i] = v.id;
                    });
                    if (ids.length === 0){
                        infoMsgError("请至少选择一个要展示的Banner！");
                    } else {
                        $.post("/banner/start", {ids: ids}, function () {
                            // 关窗口
                            layer.closeAll();
                            infoMsg("展示成功！");
                            // 重现渲染一遍table表格数据
                            $("#searchBtn").click();
                        }); //end $.post
                    }
                }); //end layer.confirm
            } // end if

            if (obj.event === 'stop') { // 点的是下架
                layer.confirm("您确定要取消展示这些商品吗？", function () {
                    let checkStactus = table.checkStatus('currentTableId');
                    let data = checkStactus.data;
                    let ids = [];
                    $.each(data, function (i, v) {
                        ids[i] = v.id;
                    });
                    if (ids.length === 0){
                        infoMsgError("请至少选择一个要取消展示的Banner！");
                    } else {
                        $.post("/banner/stop", {ids: ids}, function () {
                            // 关窗口
                            layer.closeAll();
                            infoMsg("取消展示成功！");
                            // 重现渲染一遍table表格数据
                            $("#searchBtn").click();
                        }); //end $.post
                    }
                }); //end layer.confirm
            } // end if

            if (obj.event === 'add') {
                layer.open({
                    type: 1,
                    title: "添加Banner",
                    area: ["400px", "380px"],
                    content: $("#bannerForm").html()
                });
                // 渲染表单
                form.render();
                uploadFile();
            }


        });


    }); //end layui.use
</script>

</body>
</html>